package com.woniu.micropropertyserver.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.micropropertyserver.bean.DeployTeIc;
import com.woniu.micropropertyserver.bean.DeployTenement;
import com.woniu.micropropertyserver.service.ITenementIcCheckService;
import com.woniu.micropropertyserver.util.DateUtil;
import com.woniu.micropropertyserver.util.PageRest;
import com.woniu.micropropertyserver.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * @author ：bb
 * @date ：Created in 2021/1/28 0029
 * @description：
 * @modified By：
 * @version: $
 */
@RequestMapping("/teIc")
@RestController
public class TenementIcController {

    @Autowired
    private ITenementIcCheckService tenementIcCheckServiceImpl;



    @GetMapping(value = "/findById",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findById(Long[] ids){
        System.out.println(ids);
        long id = 0;
        if(ids.length>=1){
            id=ids[0];
        }
        DeployTeIc info= tenementIcCheckServiceImpl.findById(id);
        return new ResultMsg(200,"操作成功",info);
    }

    @GetMapping(value = "/findByparams",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findByparams(PageRest pageRest ,DeployTeIc deployTeIc){
        System.out.println(deployTeIc);
        PageInfo<DeployTeIc> infos= tenementIcCheckServiceImpl.findByParams(pageRest, deployTeIc);
        return new ResultMsg(200,"操作成功",infos);
    }
    @PostMapping(value = "/save",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg save(@RequestBody DeployTeIc deployTeIc){
        System.out.println(deployTeIc);
        int res = tenementIcCheckServiceImpl.saveInfo(deployTeIc);
        if(res>0){
            return new ResultMsg(200,"操作成功",null);
        }else{
            return new ResultMsg(200,"新增失败",null);
        }

    }

    @PutMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public  ResultMsg updateAuth(@RequestBody Map map ) throws ParseException {
        System.out.println(map);
        int res=0;
        Long[] ids = new Long[((ArrayList) map.get("ids")).size()];
        Date authorizeStartDate = null;
        Date authorizeEndDate = null;
        if (map != null) {
            authorizeStartDate = DateUtil.strToDate((String) map.get("authorizeStartDate"), "yyyy-MM-dd");
            authorizeEndDate = DateUtil.strToDate((String) map.get("authorizeEndDate"), "yyyy-MM-dd");

            System.out.println(map.get("ids"));
            ArrayList strs = (ArrayList) map.get("ids");
            System.out.println("尺寸：" + strs.size());
            for (int i = 0; i < strs.size(); i++) {
                ids[i] = Long.parseLong(strs.get(i).toString());

            }
            System.out.println("日期");
            System.out.println(authorizeStartDate);
            System.out.println(ids);
            res = tenementIcCheckServiceImpl.updateAuth(ids, authorizeStartDate, authorizeEndDate);
        }
        if(res >0){
            return  new ResultMsg(200,"操作成功",null);
        }else{
            return  new ResultMsg(200,"更新失败",null);
        }

    }
    @PutMapping(value = "/updateInfo",produces = MediaType.APPLICATION_JSON_VALUE)
    public  ResultMsg updateInfo(@RequestBody DeployTeIc deployTeIc) throws ParseException {

        System.out.println(deployTeIc);

          int res = tenementIcCheckServiceImpl.updateInfo( deployTeIc);

        if(res >0){
            return  new ResultMsg(200,"操作成功",null);
        }else{
            return  new ResultMsg(200,"删除失败",null);
        }
    }
        @PutMapping(value = "/cancel",produces = MediaType.APPLICATION_JSON_VALUE)
        public ResultMsg cancelInfo(Long[] ids){
        System.out.println(ids);
        int res = tenementIcCheckServiceImpl.delInfo(ids);
        if(res >0){
            return  new ResultMsg(200,"操作成功",null);
        }else{
            return  new ResultMsg(200,"删除失败",null);
        }

    }
}
